<div class="blocktop"></div>
<!-- 直接购买 -->
<div class="blockwrap {{$private}}">
    <div class="box">
        <div class="boxtitle">
            购买确认
        </div>
        <div class="infobox">
            {{#if loginer}}
            <p>已添加商品，是否到购物车中结算？</p>
            <div class="btnbox">
                <button class="btn" event-click="{{'$Event.goto_cart'}}">直接下单</button>
                <button class="btn" event-click="{{'$Event.stay'}}">继续挑选</button>
            </div>
            {{#else}}
            <p>购买商品前请先<a href="sign_in.html?id=1{{$Cache.cb_url}}" style="color:blue;cursor:pointer">登陆</a>或者<a style="color:blue;cursor:pointer" event-click="{{'$Event.stay'}}">留在此页面</a>
            </p>
            {{/if}}
        </div>
    </div>
</div>
<div class="top_bar">
    <div class="topbar">
        <div class="bar">
            <div class="bar-left">
                <a href="admin-beta.html"><i class="icon-tag" title="已通过点纳官方认证"></i></a>
                <a href="main-beta.html" class="bar-button">首页</a>
                {{#if bus_info.info.qq}}
                <a target="_blank" href="http://wpa.qq.com/msgrd?v=3&uin={{bus_info.info.qq}}&site=qq&menu=yes">
                    <img class="chat" border="0" src="http://wpa.qq.com/pa?p=2:{{bus_info.info.qq}}:41" alt="点击这里给我发消息" title="点击这里给我发消息" />
                </a>
                <!-- /images/qqchat.gif -->
                {{/if}}
                <!-- src="http://wpa.qq.com/pa?p=2:{{bus_info.info.qq}}:51" -->
            </div>
            <div class="outbox">
                {{#if bus_info.config.notices.length!=0}}
                <noticebox data-key="bus_info.config.notices"></noticebox>
                {{/if}}
            </div>
            {{#if !$Cache.is_in_admin}}
            <div class="bar-right">
                {{#if loginer}}
                <a class="bar-button {{$Cache.is_collect_current_bus&&'icon-star3'||'icon-star'}}" title="{{$Cache.is_collect_current_bus&&'已收藏'||'点击收藏'}}" event-click="{{'$Event.user_collect_bus'}}">{{$Cache.is_collect_current_bus&&'已收藏'||'点击收藏'}}</a>
                <a href="cart-beta.html" class="bar-button icon-cart">购物车</a>
                <a id="loginer_info" class="bar-button icon-user" href="./personal-beta.html">{{loginer.info.name}}</a>
                <a class="bar-button icon-enter" event-click="{{'$Event.user_login_out'}}" id="login_out" href="#">注销</a>
                {{#else}} {{#if $Cache._in_sign_in_or_up}} {{#else}}
                <a id="sign_in_btn" href="sign_in.html?id=1{{$Cache.cb_url}}" class="bar-button border icon-user">登陆</a>
                <a id="sign_up_btn" href="sign_in.html?id=2{{$Cache.cb_url}}" class="bar-button icon-pencil">注册</a>
                {{/if}} {{/if}}
            </div>
            {{/if}}
        </div>
    </div>
</div>
<script type="text/vm">
    function (vm) {
        require(["common","coAjax","href","eventManager"], function(jSouper,coAjax,href,eventManager) {
            if (location.pathname !== "/sign_in.html") {
                var cb_url = "&cb_url=" + encodeURIComponent(location.href);
                App.set("$Cache.cb_url", cb_url);
            } else {
                App.set("$Cache._in_sign_in_or_up", true);
            }
            //注销功能
            App.set("$Event.user_login_out", function() {
                coAjax.get(appConfig.user.login_out_url,function () {
                    App.set("loginer", null);
                    if (href.isUser()) {
                        href.toLogin();
                    }
                });
            });

            //获取登陆用户的信息
            /*
             * 管理员相关的页面无需登录
             */
            var no_user_login_pages = {
                "/admin-beta.html": 1,
                "/admin-login.html": 1
            }
            if (no_user_login_pages[location.pathname]) {
                App.set("$Cache.is_in_admin",true);
                return;
            }
            console.log("获取登陆者信息……", location.pathname);
            /*
             * 获取登录者信息
             */
            function _check_collect_bus (collectBus) {
                var is_collect = true;
                if (jSouper.indexOf(collectBus,appConfig.bus_id)===-1) {
                    is_collect = false;
                }
                App.set("$Cache.is_collect_current_bus",is_collect)
            }
            coAjax.get(appConfig.user.loginer, function(data) {
                console.log("登录者信息：", data.result);
                userInfo = data.result;
                App.set("loginer", userInfo);
                //触发相关事件
                eventManager.fire("getLoginer");
                //校验是否已经收藏过此商家
                var collectBus = userInfo.collectBus||[];
                _check_collect_bus(collectBus);
            }, function(errorCode) {
                var muse_login_pages = {
                        "/personal-beta.html": 1,
                        "/cart-beta.html": 1
                    }
                    //未登录，不可进入个人页，强制跳转到登录页
                if (muse_login_pages[location.pathname]) {
                    href.toLogin();
                }
                //触发相关事件
                eventManager.reject("getLoginer");
            });

            //收藏功能
            App.set("$Event.user_collect_bus",function () {
                if (App.get("$Cache.is_collect_current_bus")) {
                    coAjax["delete"](appConfig.user.collectBus_remove,{
                        bus_id:appConfig.bus_id
                    },function (result) {
                        alert("已经取消收藏");
                        App.set('$Cache.is_collect_current_bus',false);
                        _check_collect_bus(result.result);
                    },function (errorCode,xhr,errorMsg) {
                        alert("error",errorMsg)
                    });
                }else{
                    coAjax.post(appConfig.user.collectBus_add,{
                        bus_id:appConfig.bus_id
                    },function (result) {
                        alert("success","收藏成功");
                        App.set('$Cache.is_collect_current_bus',true);
                        _check_collect_bus(result.result);
                    },function (errorCode,xhr,errorMsg) {
                        alert("error",errorMsg)
                    });
                }
            });
            App.set("$Event.top.show_bus_qrcode",function () {
                App.model.toggle("$Cache.top.is_show_bus_qrcode");
            });
        });
    }
</script>